package jrandom.algorithm.factory.adaptiveRandom;

import jrandom.algorithm.factory.ValueFactory;
import java.util.ArrayList;
import java.util.Random;


public class ARCharFactory extends ValueFactory {
	private char minValue;
	private char maxValue;
	private ArrayList<Character> charTC = new ArrayList<Character>();
	
	public ARCharFactory(int tcNum) {
		super(tcNum);
	}

	@Override
	public Object[] getValue(String min, String max) {
		String base = " 	!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~";
		try {
			if (min.toCharArray().length == 1 && max.toCharArray().length == 1) {
				minValue = min.toCharArray()[0];
				maxValue = max.toCharArray()[0];
			} else {
				throw new Exception();
			}
		} catch (Exception e) {
			minValue = base.charAt(0);
			maxValue = base.charAt(base.length()-1);
		}

		Random random = new Random();  
		if (min == null || max == null || maxValue < minValue){
			for (int i = 0; i < values.length; i++) {
				int number = random.nextInt(base.length());
				values[i] = base.charAt(number);
			}
		} else if (base.indexOf(String.valueOf(minValue)) == -1 || base.indexOf(String.valueOf(maxValue)) == -1){
			for (int i = 0; i < values.length; i++) {
				int number = random.nextInt(base.length());
				values[i] = base.charAt(number);
			}
		} else {
			for (int i = 0; i < values.length; i++) {
				String subBase = base.substring(base.indexOf(String.valueOf(minValue)), base.indexOf(String.valueOf(maxValue))+1);
				int number = random.nextInt(subBase.length());
				values[i] = base.charAt(number);
			}
		}
		return values;
	}
	
	public void addTC(Object newTC)
	{
		charTC.add((Character)newTC);
	}
	
	public double[] getDistance(Object candi)
	{
		
		int length=charTC.size();
		if(length>0)
		{
			double[] distance=new double[length];
			for(int i=0;i<length;i++)
			{
				distance[i]=Math.pow(((Character)candi)-(charTC.get(i)),2);
			}
			return distance;
		}
		else return null;
	}

}